package com.kgc.dao;

import com.kgc.entity.Gonggao;
import com.kgc.untils.PageUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GonggaoDao extends BaseDao {


    /**
     * 查所有
     *
     * @param pageUtils
     * @param name
     * @return
     */
    public List<Gonggao> selectAll(PageUtils pageUtils, String name) {
        List<Gonggao> list = new ArrayList<>();
        try {
            String sql = "SELECT g.id,g.title,g.content,a.username,g.createTime,a.username,g.updateTime,g.STATUS FROM gonggao g INNER JOIN  admin a ON g.createBy=a.id ";
            if (StringUtils.isNotEmpty(name)) {
                sql += " where title like '%" + name + "%'";
            }
            sql += "limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new Gonggao(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getTimestamp(5), rs.getString(6),
                        rs.getTimestamp(7), rs.getInt(8)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }

    /**
     * 查总数
     *
     * @param name
     * @return
     */
    public int countAll(String name) {
        try {
            String sql = "SELECT count(*) FROM gonggao ";
            if (StringUtils.isNotEmpty(name)) {
                sql += " where title like '%" + name + "%'";
            }
            ResultSet rs = super.getExecuteQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }

    /**
     * 添加
     *
     * @param gonggao
     * @return
     */
    public int addGonggao(Gonggao gonggao) {

//        String sql = "insert into gonggao"
        String sql = "INSERT INTO `gonggao`( `title`, `content`, `createBy`, `createTime`,`updateTime`, `status`) VALUES ( ?, ?, 1, ?,?,?)";
        return super.getExecuteUpdate(sql, gonggao.getTitle(), gonggao.getContent(), gonggao.getCreatetime(), gonggao.getCreatetime(), gonggao.getStatus());
    }

    /**
     * 修改
     *
     * @param gonggao
     * @return
     */
    public int updateGonggao(Gonggao gonggao) {
        String sql = "UPDATE `gonggao` SET `title` = ?, `content` = ?, `updateBy` = 1, `updateTime` = ?, `status` = ? WHERE `id` = ?";
        return super.getExecuteUpdate(sql, gonggao.getTitle(), gonggao.getContent(), gonggao.getUpdatetime(), gonggao.getStatus(), gonggao.getId());
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int deleteGonggao(String id) {
        String sql = "DELETE FROM gonggao WHERE id=?";
        return super.getExecuteUpdate(sql, id);
    }
}
